//
//  CertificationViewModel.m
//  Yihubang
//
//  Created by xmy on 16/8/28.
//  Copyright © 2016年 jinghao. All rights reserved.
//

#import "CertificationViewModel.h"
#import "TZImagePickerController.h"
@interface CertificationViewModel ()
@property (nonatomic, strong, readwrite) RACSignal *validsaveSignal;
/// The command of login button.
@property (nonatomic, strong, readwrite) RACCommand *saveCommand;
@property (nonatomic, strong, readwrite) RACCommand *updateAgainCommand;
@property (nonatomic, strong) NSString * img_url;
@end

@implementation CertificationViewModel

- (void)initialize {
    [super initialize];
    @weakify(self);
    self.updateidentificationImg =NO;
    self.validsaveSignal = [[RACSignal
                              combineLatest:@[ RACObserve(self, name), RACObserve(self, identification), RACObserve(self, updateidentificationImg)]
                              reduce:^(NSString *username, NSString *password,NSNumber * update) {
                                  return @(username.length >0 && password.length ==18 && [update boolValue]);
                              }]
                             distinctUntilChanged];
    self.saveCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        @strongify(self)
        [[[HttpManagerCenter sharedHttpManager] certificationWithname:self.name identityId:self.identification positive_photo:self.img_url opposite_photo:nil resultClass:nil] subscribeNext:^(BaseModel *model){
            if(model.code==200){
                 @strongify(self)
                [self.viewController hiddenHUDWithString:@"上传成功" error:NO];
                [[self.httpService queryUserCenterWith:nil] subscribeNext:^(BaseModel *model){
                    if (model.code==200) {
                        [[UserClient sharedUserClient] setUserInfo:model.data];
                    }
                }];
                [self.viewController.navigationController popViewControllerAnimated:YES];
            }else{
                [self.viewController hiddenHUDWithString:model.message error:NO];
            }
        }];

        return [RACSignal empty];
    }];
    self.updateAgainCommand = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(UIImageView * input) {
        @strongify(self)
//        self.updateidentificationImg =NO;
        TZImagePickerController * imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
        [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
            @strongify(self);
            UIImage * image = photos[0];
            [self.viewController showHUDWithString:@""];
            [[[HttpManagerCenter sharedHttpManager] uploadImageOne:[image toUploadString]resultClass:nil] subscribeNext:^(BaseModel *model){
                @strongify(self);
                if(model.code==200){
                   // [input setImage:image forState:UIControlStateDisabled];
                    input.image = image;
                    input.contentMode = UIViewContentModeScaleAspectFit;
                    input.backgroundColor = [UIColor clearColor];
                    self.img_url =model.data[@"url"];
                    [self.viewController hiddenHUDWithString:@"上传成功" error:NO];
                    self.updateidentificationImg =YES;
                }else{
                    [self.viewController hiddenHUDWithString:@"请重新选择图片" error:NO];
                }
            }];

        }];
        [self.viewController presentViewController:imagePickerVc animated:YES completion:nil];
        return [RACSignal empty];
    }];

}

@end
